package com.lhd.qd.module.system.dept.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhd.qd.common.base.BasePageQuery;
import com.lhd.qd.common.base.BaseServiceImpl;
import com.lhd.qd.module.system.dept.dao.DeptMapper;
import com.lhd.qd.module.system.dept.model.converter.AbstractDeptConverter;
import com.lhd.qd.module.system.dept.model.dataobj.DeptDO;
import com.lhd.qd.module.system.dept.model.dto.DeptSaveDTO;
import com.lhd.qd.module.system.dept.model.vo.DeptDetailVO;
import com.lhd.qd.module.system.dept.model.vo.DeptListVO;
import com.lhd.qd.module.system.dept.model.vo.DeptTreeVO;
import com.lhd.qd.module.system.dept.service.DeptService;
import com.lhd.qd.module.system.hospital.model.converter.AbstractHospitalConverter;
import com.lhd.qd.module.system.hospital.model.dataobj.HospitalDO;
import com.lhd.qd.module.system.hospital.model.vo.HospitalTreeVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DeptServiceImpl extends BaseServiceImpl<DeptMapper, DeptDO> implements DeptService {

    @Override
    public IPage<DeptListVO> pageDept(BasePageQuery query) {

        LambdaQueryWrapper<DeptDO> queryWrapper = Wrappers.<DeptDO>lambdaQuery();

        IPage<DeptDO> doPage = page(new Page<>(query.getCurrent(), query.getSize()), queryWrapper);

        IPage<DeptListVO> voPage = AbstractDeptConverter.INSTANCE.doPage2ListVOPage(doPage);

        return voPage;
    }

    @Override
    public List<DeptTreeVO> getDeptTreeByHospitalId(Long hospitalId) {

        LambdaQueryWrapper<DeptDO> queryWrapper = Wrappers.<DeptDO>lambdaQuery()
                .eq(DeptDO::getHospitalId, hospitalId)
                .orderByAsc(DeptDO::getParentId);

        List<DeptDO> doList = list(queryWrapper);

        return buildTree(0L, doList);
    }

    @Override
    public DeptDetailVO getDeptById(Long id) {

        DeptDetailVO vo = AbstractDeptConverter.INSTANCE.do2DetailVO(getById(id));

        return vo;
    }

    @Override
    public boolean saveDept(DeptSaveDTO saveDTO) {

        DeptDO dataObj = AbstractDeptConverter.INSTANCE.saveDTO2DO(saveDTO);

        return save(dataObj);
    }

    @Override
    public boolean updateDeptById(Long id, DeptSaveDTO saveDTO) {

        DeptDO dataObj = AbstractDeptConverter.INSTANCE.saveDTO2DO(saveDTO);

        dataObj.setId(id);

        return updateById(dataObj);
    }

    @Override
    public boolean removeDeptById(Long id) {

        return removeById(id);
    }

    private static List<DeptTreeVO> buildTree(Long parentId, List<DeptDO> doList) {

        List<DeptTreeVO> treeList =  new ArrayList<>();
        for (DeptDO dataObj : doList) {

            if (parentId.equals(dataObj.getParentId())) {

                DeptTreeVO vo = AbstractDeptConverter.INSTANCE.do2TreeVO(dataObj);
                vo.setChildren(buildTree(dataObj.getId(), doList));

                treeList.add(vo);
            }
        }

        return treeList;
    }
}
